package kiki.app;

import com.kikidao.app.exceptions.DAOCheckedExeption;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Georgiana.Scarlat
 * Date: 9/18/12
 * Time: 3:07 PM
 *
 */
public class PersonDAOImpl implements PersonDAO {

    private static final SessionFactory sessionFactory = PersonFactory.getSessionFactory();
    private String tableName = "Person";



    public void create(Person person) throws DAOCheckedExeption {
        Session session = null;

        try{

            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            session.save(person);

            transaction.commit();

        }finally {

            if(session != null)
                session.close();
        }

    }

    public Person read(Person person) throws DAOCheckedExeption {
        Session session = null;

        try{

            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            //noinspection unchecked
            List<Person> result = session.createQuery("from "+tableName+" as pers where pers.id = "+person.getId()).list();

            transaction.commit();

            validateReadResult(result);
            return result.get(0);

        }finally {

            if(session != null)
                session.close();
        }
    }


    public void update(Person person) throws DAOCheckedExeption {
        Session session = null;

        try{

            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            session.update(person);

            transaction.commit();

        }finally {

            if(session != null)
                session.close();
        }
    }

    public void delete(Person person) throws DAOCheckedExeption {

        Session session = null;

        try{

            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            //noinspection unchecked
            int deleted = session.createQuery("delete "+tableName+" as pers where pers.id = "+person.getId()).executeUpdate();

            transaction.commit();
            validateDelete(deleted);


        }finally {

            if(session != null)
                session.close();
        }

    }


    public List<Person> list() throws DAOCheckedExeption {
        Session session = null;

        try{

            session = sessionFactory.openSession();
            Transaction transaction = session.beginTransaction();

            //noinspection unchecked
            List<Person> result = session.createQuery("from "+tableName).list();

            transaction.commit();
            return result;

        }finally {

            if(session != null)
                session.close();
        }
    }

    private void validateReadResult(List<Person> result) throws DAOCheckedExeption {
        if(result == null || result.size() != 1)
            throw new DAOCheckedExeption("Invalid read!");
    }

    private void validateDelete(int deleted) throws DAOCheckedExeption {
        if(deleted != 1)
            throw new DAOCheckedExeption("Cannot delete person.");
    }


}
